﻿using System.Collections.Generic;
using UnityEngine;
using System;


namespace CSV_Tool
{
    //用于从Resources中加载CSV表格的类
    public static class CSVLoader
    {
        //在Resources/CSV文件夹中加载CSV,并把CSV转换成二维数组
        public static string[,] LoadCSVData(string loadName)
        {
            string[,] returnValue = null;
            var csvAsset = Resources.Load<TextAsset>("CSVData/" + loadName);

            string[] line = csvAsset.text.Split('\r');
            //老版本，新版能用就删了
            string[] row = GetRowFromRes(line[0]);

            returnValue = new string[line.Length - 2, row.Length];

            for (int count = 0; count < returnValue.GetLength(0); count++)
            {
                var dataRow = GetRowFromRes(line[count + 1],row.Length);
                for (int count2 = 0; count2 < returnValue.GetLength(1); count2++)
                {
                    returnValue[count, count2] = dataRow[count2].TrimStart();
                }
            }

            return returnValue;
        }

        //在Resources/CSV文件夹中加载CSV,并把CSV转换成String数组的数组
        public static string[][] LoadCSVDataArrayLayer(string loadName)
        {
            string[][] returnValue = null;
            var csvAsset = Resources.Load<TextAsset>("CSVData/" + loadName);

            string[] line = csvAsset.text.Split('\r');
            string[] row = line[0].Split(',');

            returnValue = new string[line.Length - 2][];

            for (int count = 0; count < returnValue.Length; count++)
            {
                returnValue[count] = line[count + 1].Split(',');
                for (int count2 = 0; count2 < returnValue[count].Length; count2++)
                {
                    returnValue[count][count2].TrimStart();
                }
            }
            return returnValue;
        }

        private static string[] GetRowFromRes(string strRes,int formatCount = 0)
        {
            var splitArray = strRes.Split(new string[1] { "\"" }, StringSplitOptions.None);

            var strList = new List<string>();

            for (int i = 0; i < splitArray.Length; i++)
            {
                var element = splitArray[i];

                if (element[0] == ',' || element[element.Length - 1] == ',')
                {
                    element = element.TrimStart(',').TrimEnd(',');
                    var elements = element.Split(',');
                    for (int count = 0; count < elements.Length; count++)
                    {
                        var selectItem = elements[count];
                        strList.Add(selectItem);
                    }
                }
                else
                    strList.Add(element);
            }

            if(formatCount!= 0 && strList.Count<formatCount)
            {
                var fixLen = strList.Count-1;
                for(int i = fixLen; i<formatCount; i++)
                    strList.Add(string.Empty);
            }

            return strList.ToArray();
        }
    }
}
